/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Base.Dados;
import Controle.ControlePergunta;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Guilherme
 */
public class Relatorio extends javax.swing.JFrame {

    /**
     * Creates new form Relatorio
     */
    public Relatorio() {
        initComponents();
        cp = new ControlePergunta();
        preencherTabela();
    }
    
    public void preencherTabela(){
        lista = cp.buscarDados();   
        
        Object[] linha = new Object[2];
        modeloTabela = (DefaultTableModel) tblquestao.getModel();
        tblquestao.setModel(modeloTabela);
        for (int i = 0; i < lista.size(); i++) {
            linha[0] = lista.get(i).getTema();
            linha[1] = lista.get(i).getPergunta();
            modeloTabela.addRow(linha);
        }
    }
   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlRelatorio = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblquestao = new javax.swing.JTable();
        btnAddQ = new javax.swing.JButton();
        btnDelQ = new javax.swing.JButton();
        btnEditQ = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        pnlvisualizar = new javax.swing.JTextPane();
        lblErro = new javax.swing.JLabel();
        btnMenu = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Responda e Aprenda - Relátorio de Questões");
        setResizable(false);

        pnlRelatorio.setPreferredSize(new java.awt.Dimension(920, 583));

        tblquestao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tema", "Pergunta"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblquestao.getTableHeader().setReorderingAllowed(false);
        tblquestao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblquestaoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblquestao);

        btnAddQ.setText("Incluir Nova Questão");
        btnAddQ.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        btnAddQ.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnAddQ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddQActionPerformed(evt);
            }
        });

        btnDelQ.setText("Excluir Questão Selecionada");
        btnDelQ.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        btnDelQ.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDelQ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelQActionPerformed(evt);
            }
        });

        btnEditQ.setText("Alterar Questão Selecionada");
        btnEditQ.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        btnEditQ.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnEditQ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditQActionPerformed(evt);
            }
        });

        pnlvisualizar.setBackground(new java.awt.Color(240, 240, 240));
        pnlvisualizar.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Questão", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        pnlvisualizar.setEditable(false);
        jScrollPane5.setViewportView(pnlvisualizar);

        lblErro.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        lblErro.setForeground(new java.awt.Color(255, 0, 0));
        lblErro.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblErro.setText("Selecione algum item na tabela");

        btnMenu.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar.png"))); // NOI18N
        btnMenu.setText("Voltar");
        btnMenu.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        btnMenu.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnMenu.setPreferredSize(new java.awt.Dimension(160, 40));
        btnMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMenuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlRelatorioLayout = new javax.swing.GroupLayout(pnlRelatorio);
        pnlRelatorio.setLayout(pnlRelatorioLayout);
        pnlRelatorioLayout.setHorizontalGroup(
            pnlRelatorioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRelatorioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlRelatorioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlRelatorioLayout.createSequentialGroup()
                        .addGroup(pnlRelatorioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnlRelatorioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(btnAddQ, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnDelQ, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnEditQ, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(lblErro, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE))
                    .addGroup(pnlRelatorioLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnlRelatorioLayout.setVerticalGroup(
            pnlRelatorioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRelatorioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlRelatorioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlRelatorioLayout.createSequentialGroup()
                        .addComponent(lblErro, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAddQ)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelQ)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditQ)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane5))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 509, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(btnMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 930, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(pnlRelatorio, javax.swing.GroupLayout.PREFERRED_SIZE, 899, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 31, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 594, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnlRelatorio, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE))
        );

        pnlRelatorio.getAccessibleContext().setAccessibleName("Responda e Aprenda - Relátorio de Questões");

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-946)/2, (screenSize.height-632)/2, 946, 632);
    }// </editor-fold>//GEN-END:initComponents

    private void tblquestaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblquestaoMouseClicked
        Dados selec = lista.get(tblquestao.getSelectedRow());
        
        String respCor;
        if(selec.getResp1().isCorreta())
        {
            respCor = "A";
        }
        else if(selec.getResp2().isCorreta())
        {
            respCor = "B";
        }
        else if(selec.getResp3().isCorreta())
        {
            respCor = "C";
        }
        else
        {
            respCor = "D";
        }
        pnlvisualizar.setText("QUESTÃO SELECIONADA: \n==================================="
                + "\nTema: "+selec.getTema().getDescricao()
                +"\n\nPergunta:\n"+selec.getPergunta().getPergunta()+"\n\nRespostas:\nA-"+
                selec.getResp1().getDescricao()+"\nB-"+selec.getResp2().getDescricao()+
                "\nC-"+selec.getResp3().getDescricao()+"\nD-"+selec.getResp4().getDescricao()
                +"\n\nResposta Correta: "+respCor+"\n\nComentário: "+selec.getPergunta().getComentario()
                +"\n\nImagem: "+selec.getPergunta().toImagem()+"\n\nLink: "+selec.getPergunta().getLink());
        
        
    }//GEN-LAST:event_tblquestaoMouseClicked

    private void btnAddQActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddQActionPerformed
        Adm a = new Adm();
        a.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnAddQActionPerformed

    private void btnDelQActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelQActionPerformed
        int selecionado = tblquestao.getSelectedRow();
        Dados selec = lista.get(selecionado);
        
        boolean resultado = cp.delete(selec);
        
        if(resultado)
        {
            JOptionPane.showMessageDialog(rootPane, "Questão excluída com Sucesso!", "SUCESSO", JOptionPane.INFORMATION_MESSAGE);                        
            modeloTabela.removeRow(selecionado);
        }
        else
        {
            JOptionPane.showMessageDialog(rootPane, "A questão não foi excluída!\nTente novamente!", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
        pnlvisualizar.setText("");
        
    }//GEN-LAST:event_btnDelQActionPerformed

    private void btnEditQActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditQActionPerformed
        if(tblquestao.getSelectedRow() == -1){
            return;
        }
        Dados selec = lista.get(tblquestao.getSelectedRow());
        Adm adm = new Adm();
        adm.setDados(selec);
        this.setVisible(false);        
        adm.setVisible(true);
        adm.preencher();
    }//GEN-LAST:event_btnEditQActionPerformed

    private void btnMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMenuActionPerformed
       Inicial ini = new Inicial();
       ini.setVisible(true);
       this.setVisible(false);
    }//GEN-LAST:event_btnMenuActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Relatorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Relatorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Relatorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Relatorio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Relatorio().setVisible(true);
            }
        });
    }
    
    private ControlePergunta cp;
    private ArrayList<Dados> lista;
    private DefaultTableModel modeloTabela;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddQ;
    private javax.swing.JButton btnDelQ;
    private javax.swing.JButton btnEditQ;
    private javax.swing.JButton btnMenu;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JScrollPane jScrollPane5;
    private static javax.swing.JLabel lblErro;
    private javax.swing.JPanel pnlRelatorio;
    private static javax.swing.JTextPane pnlvisualizar;
    private static javax.swing.JTable tblquestao;
    // End of variables declaration//GEN-END:variables
}
